package com.jdym.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdym.R;
import com.jdym.back.entity.Business;
import com.jdym.back.entity.Manage;
import com.jdym.back.entity.Menu;
import com.jdym.back.entity.vo.ManageRoleMenu;
import com.jdym.back.service.BusinessService;
import com.jdym.back.service.ManageService;
import com.jdym.back.service.MenuService;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 后台管理员表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@RestController
@CrossOrigin
@RequestMapping("/back/manage")
public class ManageController {
    @Autowired
    ManageService manageService;

    @Autowired
    BusinessService businessService;

    @Autowired
    MenuService menuService;

     String MUser;
    String MPwd;
    String BUser;
    String BPwd;
    @RequestMapping("/getManageById/{manageId}")
    public R getManageById(@PathVariable int manageId) {
        Manage manage = manageService.getBaseMapper().selectById(manageId);
        return R.ok().data("manage", manage);
    }




    @RequestMapping("/login/{username}/{password}")
    public R login(@PathVariable String username, @PathVariable String password, HttpSession session) {
        Manage manage = manageService.getBaseMapper().selectOne(new QueryWrapper<Manage>().eq("name", username).eq("pwd", password));
        System.out.println("123123:" + manage);
        System.out.println("1:"+MUser);

        if(manage!=null){
            MUser = manage.getMname();

            return R.ok().data("token", "admin").data("manageid",manage.getId());
        }else {
            return R.error();
        }

    }

    @GetMapping("/info")
    public R info() {
        System.out.println("getInfo被调用了");

        if(MUser!=null){
//            根据用户名查找权限菜单
            List<ManageRoleMenu> manageRoleMenus=menuService.getMenus(MUser);//一级菜单
//            二级菜单
            for (int i=0;i<manageRoleMenus.size();i++) {
               List<Menu> sub= menuService.findById(manageRoleMenus.get(i).getMenuId());
                manageRoleMenus.get(i).setChildren(sub);
            }

            return R.ok().data("menus",manageRoleMenus)
                    .data("roles", "[admin]")
                    .data("name", "admin")
                    .data("avatar", "http://localhost:8010/upload/5cbcdb9fb0ea4567b2ee0698da7faf31-圣诞节-圣诞树.png");
        }else {
            return R.error();
        }
    }

//    基本信息修改及密码修改
    @PostMapping("/updateManegeInfo/{id}/{name}/{phone}/{pwd}/{email}")
    public R UpadateManageInfo(@PathVariable int id,@PathVariable String name,@PathVariable String phone,@PathVariable String pwd,@PathVariable String email){
      Manage manage=  new Manage();
      manage.setId(id);
      manage.setMname(name);
      manage.setPwd(pwd);
      manage.setEmail(email);
      manage.setPhone(phone);
        System.out.println("我想看看接收到的manage是什么："+manage);
        manageService.getBaseMapper().updateById(manage);
        return R.ok().data("msg","修改成功");
    }

//   查找所有的管理员
    @GetMapping(value = "/findAllUser")
    public R findAllUser(){
        List<Manage> manages = manageService.getBaseMapper().selectList(null);
        System.out.println("manages:"+manages);
        return R.ok().data("manages",manages);
    }

    @GetMapping(value = "/updateShowUser/{id}")
    public R updateShowUser(@PathVariable int id){
        Manage manage=manageService.getBaseMapper().selectById(id);
        System.out.println("要修改的用户："+manage);
        return R.ok().data("manage",manage);
    }
//更新或新增
    @PostMapping(value = "/updateUser")
    public R updateUser(@RequestParam(required = false) Integer id, String mname,String pwd,String email,String phone,@RequestParam(required = false) Integer roleId){
        System.out.println("传过来的form表单"+mname);
        Manage manage=new Manage();
        manage.setMname(mname);
        manage.setPwd(pwd);
        manage.setEmail(email);
        manage.setPhone(phone);
        if(roleId!=null){
            manage.setRoleId(roleId);
        }
        if(id!=null){
            manage.setId(id);

            manageService.getBaseMapper().updateById(manage);
        }else {
            manageService.getBaseMapper().insert(manage);
        }
        return R.ok();
    }
//    删除管理员用户
    @DeleteMapping(value = "/delUser/{id}")
    public R delUser(@PathVariable int id){
        manageService.getBaseMapper().deleteById(id);
        return R.ok();
    }
}

